home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue33 / construc / DRBOBCGI.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-04-06  |  2.6 KB  |  120 lines

  1. unit DrBobCGI;
  2. {$I-}
  3. interface
  4.  
  5. type
  6.   TRequestMethod = (Unknown,Get,Post);
  7. var
  8.   RequestMethod: TRequestMethod = Unknown;
  9.  
  10. var
  11.   ContentLength: Integer = 0;
  12.   Data: AnsiString = '';
  13.  
  14.   function Value(const Field: ShortString): ShortString;
  15.  
  16.   function ValueAsInteger(const Field: ShortString): Integer;
  17.  
  18. implementation
  19. uses
  20.   SysUtils, Windows;
  21.  
  22.   function Value(const Field: ShortString): ShortString;
  23.   var
  24.     i: Integer;
  25.     len: Byte absolute Result;
  26.   begin
  27.     Len := 0;
  28.     i := Pos('&'+Field+'=',Data);
  29.     if i = 0 then
  30.     begin
  31.       i := Pos(Field+'=',Data);
  32.       if i > 1 then i := 0
  33.     end
  34.     else Inc(i); { skip '&' }
  35.     if i > 0 then
  36.     begin
  37.       Inc(i,Length(Field)+1);
  38.       while Data[i] <> '&' do
  39.       begin
  40.         if not (Data[i] in [#10,#13]) then { ignore CR/LF }
  41.         begin
  42.           Inc(Len);
  43.           Result[Len] := Data[i]
  44.         end
  45.         else { CR/LF -> #32 }
  46.         begin
  47.           if (Len = 0) or (Result[Len] <> #32) then
  48.           begin
  49.             Inc(Len);
  50.             Result[Len] := #32
  51.           end
  52.         end;
  53.         Inc(i)
  54.       end
  55.     end;
  56.     while (Len > 0) and (Result[len] = #32) do Dec(len)
  57.   end {Value};
  58.  
  59.   function ValueAsInteger(const Field: ShortString): Integer;
  60.   begin
  61.     Result := StrToInt(Value(Field))
  62.   end {ValueAsInteger};
  63.  
  64. var
  65.   P: PChar;
  66.   i: Integer;
  67.   Str: ShortString;
  68.  
  69. initialization
  70.   P := GetEnvironmentStrings;
  71.   while P^ <> #0 do
  72.   begin
  73.     Str := StrPas(P);
  74.     if Pos('REQUEST_METHOD=',Str) > 0 then
  75.     begin
  76.       Delete(Str,1,Pos('=',Str));
  77.       if Str = 'POST' then RequestMethod := Post
  78.       else
  79.         if Str = 'GET' then RequestMethod := Get
  80.     end;
  81.     if Pos('CONTENT_LENGTH=',Str) = 1 then
  82.     begin
  83.       Delete(Str,1,Pos('=',Str));
  84.       ContentLength := StrToInt(Str)
  85.     end;
  86.     if Pos('QUERY_STRING=',Str) > 0 then
  87.     begin
  88.       Delete(Str,1,Pos('=',Str));
  89.       SetLength(Data,Length(Str)+1);
  90.       Data := Str
  91.     end;
  92.     Inc(P, StrLen(P)+1)
  93.   end;
  94.   if RequestMethod = Post then
  95.   begin
  96.     SetLength(Data,ContentLength+2);
  97.     for i:=1 to ContentLength do read(Data[i]);
  98.     Data[ContentLength+1] := '&';
  99.   { if IOResult <> 0 then { skip }
  100.   end;
  101.   i := 0;
  102.   while i < Length(Data) do
  103.   begin
  104.     Inc(i);
  105.     if Data[i] = '+' then Data[i] := ' ';
  106.     if Data[i] = '%' then { special code }
  107.     begin
  108.       Str := '$00';
  109.       Str[2] := Data[i+1];
  110.       Str[3] := Data[i+2];
  111.       Delete(Data,i+1,2);
  112.       Data[i] := Chr(StrToInt(Str))
  113.     end
  114.   end;
  115.   if i > 0 then Data[i+1] := '&'
  116.            else Data := '&'
  117. finalization
  118.   Data := ''
  119. end.
  120.